<?php


/**
 * Use a static class
 *
 */
class Vega_Language 
{
    
    protected static $_files = array();
    
    protected static $_cacheFiles = array();
    
    protected static $_cacheData = array();
    
    protected static $_language = 'en';
    
    private function __construct()
    {
        
    }
    
    /**
     * Sets language
     *
     * @param string $language
     */
    public static function setLanguage($language)
    {
        self::$_language = $language;
    }
    
    /**
     * Adds a language data file
     *
     * @param string $name
     * @param string $language
     */
    public static function addFile($name, $language = null)
    {
        if (null == $language)
        {
            $language = self::$_language;
        }
        
        self::$_files[$language][] = $name;
    }
    
    /**
     * Resets a language object
     *
     * @return boolean
     */
    public static function resetLanguage($language)
    {
        if (!isset(self::$_files[$language]))
        {
            return false;
        }
        self::$_files[$language] = array();
        unset(self::$_cacheData);
        return true;
    }
       
    /**
     * Get a language object
     *
     * @param string $name
     * @return array
     */
    public static function getData($language = null)
    {
    	if (null == $language)
        {
            $language = self::$_language;
        }
        
        if (!isset(self::$_files[$language]))
        {
            require_once 'Vega/Language/Exception.php';
            throw new Vega_Language_Exception('Language with name ' . $language . ' does not exist.');
        }
        
        // return cached object
        if (isset(self::$_cacheData[$language]) && count(self::$_files[$language]) == count(self::$_cacheFiles[$language]))
        {
            return self::$_cacheData[$language];
        }
        
        static $loaderInited = false;
        if (true)
        {
            $loader = Vega_App::getInstance()->getLoader();
            $loader->setDir('language', 'language');
            $loaderInited = true;
        }
        
        $_lang = isset(self::$_cacheData[$language]) ? self::$_cacheData[$language] : array();
        
        if (!empty(self::$_files[$language]))
        {
            foreach (self::$_files[$language] as $file)
            {
            	if(!isset(self::$_cacheFiles[$language]) || !in_array($file, (array)self::$_cacheFiles[$language]))
            	{	
                	$tempLang = $loader->load('language', $language . DIRECTORY_SEPARATOR . $file . '.lang.php', '_lang', TRUE);
             	   	$_lang = array_merge($_lang, (array)$tempLang);
            	}
            }
        }
        
        self::$_cacheData[$language] 	= $_lang;
        self::$_cacheFiles[$language] 	= self::$_files[$language];
        
        return $_lang;
    }
    
}


